#include "NodeGraphicsScene.h"

#include <QLine>
#include <QtMath>
#include <QVector>
#include <QPainter>

QDMGraphics::QDMGraphics(QGraphicsScene *parent)
    :QGraphicsScene(parent)
{
    m_penLight = new QPen(QColor(0x2f2f2f));
    m_penLight->setWidth(1);
    m_penDark = new QPen(QColor(0x292929));
    m_penDark->setWidth(2);

    setSceneRect(-c_SceneWidth / 2, -c_SceneHeight / 2, c_SceneWidth, c_SceneHeight);
    setBackgroundBrush(QColor(0x393939));
}

void QDMGraphics::drawBackground(QPainter *painter, const QRectF &rect)
{
    QGraphicsScene::drawBackground(painter, rect);

    int left = int(qCeil(rect.left()));
    int right = int(qFloor(rect.right()));
    int top = int(qCeil(rect.top()));
    int bottom = int(qFloor(rect.bottom()));

    int first_left = left - (left % c_GridSize);
    int first_top = top - (top % c_GridSize);

    QVector<QLine> linesLight, linesDark;
    for (int x = first_left; x <= right; x += c_GridSize) {
        if (x % (c_GridSize * c_GridSquares) != 0) {
            linesLight.push_back(QLine(x, top, x, bottom));
        } else {
            linesDark.push_back(QLine(x, top, x, bottom));
        }
    }
    for (int y = first_top; y <= bottom; y += c_GridSize) {
        if (y % (c_GridSize * c_GridSquares) != 0){
            linesLight.push_back(QLine(left, y, right, y));
        } else {
            linesDark.push_back(QLine(left, y, right, y));
        }
    }

    painter->setPen(*m_penLight);
    painter->drawLines(linesLight);

    painter->setPen(*m_penDark);
    painter->drawLines(linesDark);

}
